//
//  PreviewTutorialView.m
//  TutorialsModule
//
//  Created by Augusto Guido on 03/10/09.
//  Copyright 2009 Moove-IT. All rights reserved.
//

#import "PreviewTutorialView.h"
#import "PreviewImage.h"
#import <QuartzCore/QuartzCore.h>

@implementation PreviewTutorialView
@synthesize toolbar;
@synthesize playing;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		
		self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
		toolbar = [[UIToolbar alloc] init];
		[toolbar setBarStyle:UIBarStyleBlackTranslucent];		
		
		UIBarButtonItem *b = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(stopPreview)];
		
		NSArray *itemsToolBar = [[NSArray alloc] initWithObjects:b,nil];
		
		toolbar.items = itemsToolBar;
		[toolbar setAlpha:0.5];		
		toolbar.translucent = YES;
		
		
		
		UIImage *image1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1.jpg" ofType:nil]];
		view1 = [[UIImageView alloc] initWithImage:image1];
		UIImage *image2 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2.jpg" ofType:nil]];
		view2 = [[UIImageView alloc] initWithImage:image2];
		view2.hidden = YES;
		
		[view1 setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
		[view2 setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
		
		[self.view addSubview:view1];
		[self.view addSubview:view2];
		[self.view addSubview:naviBar];
		[self.view addSubview:toolbar];
		
		UIBarButtonItem *u = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(stopPreview)];
		naviBar.topItem.leftBarButtonItem = u;
		naviBar.hidden = YES;
		toolbar.hidden = YES;
		
		transitioning = NO;
		playing = YES;
		[NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(nextTransition) userInfo:nil repeats:YES];
		
		//[image1 release];
		//[image2 release];
		
		[b release];
		[u release];
		[naviBar release];
		[toolbar release];
		[itemsToolBar release];
		
		
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	naviBar.hidden = NO;
	toolbar.hidden = NO;
	playing = NO;
	[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(hideToolbar) userInfo:nil repeats:NO];
}

-(void) hideToolbar{
	naviBar.hidden = YES;
	toolbar.hidden = YES;
	[self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (IBAction)stopPreview {
	playing = NO;
    [self dismissModalViewControllerAnimated:YES];
}



-(void)performTransition
{
	// First create a CATransition object to describe the transition
	CATransition *transition = [CATransition animation];
	// Animate over 3/4 of a second
	transition.duration = 0.75;
	// using the ease in/out timing function
	transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
	
	// Now to set the type of transition. Since we need to choose at random, we'll setup a couple of arrays to help us.
	transition.type = kCATransitionMoveIn;
	transition.subtype = kCATransitionFromRight;
	
	// Finally, to avoid overlapping transitions we assign ourselves as the delegate for the animation and wait for the
	// -animationDidStop:finished: message. When it comes in, we will flag that we are no longer transitioning.
	transitioning = YES;
	transition.delegate = self;
	
	// Next add it to the containerView's layer. This will perform the transition based on how we change its contents.
	[self.view.layer addAnimation:transition forKey:nil];
	
	// Here we hide view1, and show view2, which will cause Core Animation to animate view1 away and view2 in.
	view1.hidden = YES;
	view2.hidden = NO;
	
	// And so that we will continue to swap between our two images, we swap the instance variables referencing them.
	UIImageView *tmp = view2;
	view2 = view1;
	view1 = tmp;
}

-(IBAction)next{
	if (!transitioning)
		[self performTransition];
}

-(IBAction)continuePreview{
	playing = YES;
	[self nextTransition];
}


-(void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
	transitioning = NO;
}

-(IBAction)nextTransition
{
	if(!transitioning && playing)
	{
		[self performTransition];
	}
	
}




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
	return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
	if (toInterfaceOrientation == UIInterfaceOrientationPortrait){
		[view1 setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
		[view2 setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
	}
	else{
		[view1 setFrame:CGRectMake(10.0f, 10.0f, 150.0f, 200.0f)];
		[view2 setFrame:CGRectMake(10.0f, 10.0f, 150.0f, 200.0f)];
	}	
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	NSLog(@"ahora");
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


-(void)dealloc
{
	[view1 release];
	[view2 release];

	[toolbar release];
	[image release];
	[naviBar release];
	
	[super dealloc];
}


@end
